import urllib.request
import json
import os

listFilePath = "bookList_{}.json"
# bookDetail_{publishingId}_{bookId}.json
bookFilePath = "{}/bookDetail_{}_{}.json"
bookDetailUrl = "http://www.goodfatherapp.com/book/bookDetail?bookId={}&publishingId={}"
types = [{"value": "1", "desc": "课本"}, {"value": "3", "desc": "课外读物"}]

for ind, type in enumerate(types):
    fList = open(listFilePath.format(type["value"]), mode='r')
    dataList = json.loads(fList.read())
    os.mkdir(type["value"])
    for ind, bookInfo in enumerate(dataList['data']):
        bookId = bookInfo["id"]
        publishingId = bookInfo["publishing_id"]
        bookDetail = urllib.request.urlopen(bookDetailUrl.format(bookId, publishingId)).read()
        fBook = open(bookFilePath.format(type["value"], publishingId, bookId), mode="w")
        print(bookDetail.decode('utf-8'), file=fBook)
